import 'package:flutter/material.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/instance_manager.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/common/input_item.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/my_controller/authentication_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/upload_util.dart';
import 'package:xinxuan/vo/upload_image_vo.dart';
import 'package:get/get.dart';

class AuthenticationPage extends StatelessWidget {

  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AuthenticationPageController>(
      init: AuthenticationPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '实名认证',
            backgroundColor: pageBgColor,
          ),
          body: GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: ScrollConfigurationCommon(child: ListView(
              children: [
                InputItem(
                  title: '真实姓名', 
                  hintText: '请输入真实姓名',
                  defaultValue: controller.realname,
                  onChenge: (String value) {
                    controller.realname = value;
                  }, 
                ),
                Obx(() => InputItem(
                  title: '身份证号', 
                  hintText: '请输入身份证号码', 
                  defaultValue: controller.number.value,
                  // keyBoardType: TextInputType.number, 
                  onChenge: (String value) {
                    controller.number.value = value;
                  }
                )),
                Obx(() => _imageItem(title: '身份证\n正面图片', style: 1, context: context, path: controller.frontOfIdCardPath.value, controller: controller)),
                Obx(() => _imageItem(title: '身份证\n反面图片', style: 2, context: context, showBorder: false, path: controller.reverseSideOfIdCardPath.value, controller: controller)),
              ],
            )),
          ),
          bottomNavigationBar: BottomAppBar(
            child: Container(
              height: 56.h,
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              alignment: Alignment.center,
              child: ButtonUtil(
                text: '立即认证', 
                fontSize: 16.sp, 
                borderRadius: 30.w, 
                height: 36.h, 
                onTop: () {
                  controller.userAuthentication();
                }
              ),
            )
          )
        );
      },
    );
  }

  Widget _imageItem({ required String title, required int style, required BuildContext context, required AuthenticationPageController controller, String path = '', bool showBorder = true}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        height: 99.h,
        decoration: BoxDecoration(
          border: Border(
            bottom: showBorder ? BorderSide(
              width: .5,
              color: lineColor
            ) : BorderSide.none
          )
        ),
        child: Row(
          children: [
            Container(
              width: 84.w,
              child: Text(
                title,
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 16.sp
                ),
              ),
            ),
            InkWellUtil.shakeProofInkWell(path.isEmpty ? Image.asset(
              'assets/icons/icon_upload_image.png',
              width: 67.w,
              height: 67.w,
            ) : NetworkImageUtil(
              url: path, 
              width: 67.w,
              height: 67.w,
              borderRadius: 5.w,
            ), () async {
              showSelectPhoto(context, style, controller);
            })
          ],
        ),
      ),
    );
  }

  showSelectPhoto(BuildContext context, int style, AuthenticationPageController controller) {
    FocusScope.of(context).requestFocus(FocusNode());
    Get.bottomSheet(Container(
      height: 168.h + ScreenUtil().bottomBarHeight,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.vertical(top: Radius.circular(5.w)),
        color: bgWhiteColor
      ),
      child: Column(
        children: [
          _selectItem(title: '拍照', onTap: () async {
            bool res = await commonPageController.checkCameraPermission();
            if (!res) {
              return;
            }
            Get.back();
            List<UploadImageData> imageArr = await UploadUtil().uploadImageFromCamera(style: style);
            if (imageArr.length <= 0) {
              return;
            }
            if (style == 1) {
              controller.frontOfIdCard = imageArr[0].id!;
              controller.frontOfIdCardPath.value = imageArr[0].image!;
            } else {
              controller.reverseSideOfIdCard = imageArr[0].id!;
              controller.reverseSideOfIdCardPath.value = imageArr[0].image!;
            }
          }),
          _selectItem(title: '相册', onTap: () async {
            bool res = await commonPageController.checkPhotosPermission();
            if (!res) {
              return;
            }
            Get.back();
            List<UploadImageData> imagesId = await UploadUtil().uploadImage(context: context, maxAssets: 1, style: style);
            if (imagesId.length <= 0) {
              return;
            }
            if (style == 1) {
              controller.frontOfIdCard = imagesId[0].id!;
              controller.frontOfIdCardPath.value = imagesId[0].image!;
            } else {
              controller.reverseSideOfIdCard = imagesId[0].id!;
              controller.reverseSideOfIdCardPath.value = imagesId[0].image!;
            }
          }),
          _selectItem(title: '取消', isCancel: true, onTap: () {
            Get.back();
          }),
        ],
      ),
    ));
  }

  Widget _selectItem({ required String title, required Function onTap, bool isCancel = false}) {
    return InkWellUtil.shakeProofInkWell(Container(
      alignment: Alignment.center,
      height: 56.h,
      decoration: BoxDecoration(
        border: Border(
          bottom: !isCancel ? BorderSide(
            width: .5,
            color: lineColor
          ) : BorderSide.none
        )
      ),
      child: Text(
        title,
        style: TextStyle(
          color: isCancel ? mainTextColor : textBlackColor,
          fontSize: 16.sp
        ),
      ),
    ), () => onTap());
  }
}